package String;

import java.util.Arrays;

public class Stringdefor {
    //时间复杂度NlgN，排序的时间花费较大
    public boolean is_Stringdefor(String A,String B){
        int len1=A.length();
        int len2=B.length();
        if(len1!=len2){
            return false;
        }
        //转成字符数组
        char[] ch1=A.toCharArray();
        char[] ch2=B.toCharArray();
        //对字符数组进行排序
        Arrays.sort(ch1);
        Arrays.sort(ch2);
        return ch1.equals(ch2);
    }
    public boolean is_Stringdefor_02(String A,String B){
        int[] helper=new int[128];
        //扫描A字符串
        for(int i=0;i<A.length();i++){
            int flag=(int)A.charAt(i);
            helper[flag]++;
        }
        //扫描B字符串
        for(int i=0;i<B.length();i++){
            int flag=(int)B.charAt(i);
            if(helper[flag]<0){
                return false;
            }else{
                helper[flag]--;
            }
        }
        //遍历辅助数组
        for(int i=0;i<helper.length;i++){
            if(helper[i]!=0){
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        Stringdefor stringdefor=new Stringdefor();
        System.out.println(stringdefor.is_Stringdefor_02("Here you are","Are you here"));
    }
}
